/**
 * 季返报表
 * @author miaoyl
 * @date 2019/05/07
 */
define(function () {
    var date = new Date();
    var END = date.Format("yyyy-MM-dd");
    var START;
    if((date.getMonth() + 1) <= 3){
        START = date.getFullYear()+'-01-01';
    }else if((date.getMonth() + 1) <= 6){
        START = date.getFullYear()+'-04-01';
    }else if((date.getMonth() + 1) <= 9){
        START = date.getFullYear()+'-07-01';
    }else {
        START = date.getFullYear()+'-10-01';
    }
    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_where = {},
        cols = [[
            {field: 'GWDD012', width: 160, title: '客户编号', fixed: 'left',sort: true,search:'default',searchOpts: {icon: true}}
            , {field: 'GWAKL003', width: 160, title: '客户名称'}
            , {field: 'GWAL013_NAME', width: 160, title: '省份'}
            , {field: 'MA041', width: 160, title: '结算方式'}
            , {field: 'GWDD017_NAME', width: 160, title: '返利编号',search: 'default',
                searchOpts: {icon: true}}
            , {field: 'GWAHL003', width: 160, title: '系列'}
            , {field: 'FHJE', width: 160, title: '发货金额'}
            , {field: 'THJE', width: 160, title: '退货金额'}
            , {field: 'HKJE', width: 160, title: '回款金额'}
            , {field: 'YQHK', width: 160, title: '逾期回款'}
            , {field: 'WFLFH', width: 160, title: '无返利发货'}
            , {field: 'PHFH', width: 160, title: '铺货发货'}
            , {field: 'PHHK', width: 160, title: '铺货回款'}
            , {field: 'PHWFL', width: 160, title: '铺货无返利回款'}
            , {field: 'YJFHJE', width: 160, title: '应记发货金额'}
            , {field: 'GWDD019', width: 160, title: '正常返点率'}
            , {field: 'ZCFLJE', width: 160, title: '正常返利金额'}
        ]],
        moduleName = '季返报表',
        COMPANY
    ;

    app.newModule({
        init: function () {
            self = this;
            var _searchsArr = app.cloneJSON(cols[0]);
            self._super({
                _tag: 'gwai790',
                _searchs: [{
                    id: 'gwai790', tableId: 'gwai790_table', datas:  _searchsArr.pushArray([
                        {
                            field: 'COMPANY', title: '法人',search: 'default',
                            searchOpts: {icon: true}
                        },
                        {
                            field: 'DOCDATE', title: '日期范围',search: 'default',
                            searchOpts: {type: 'daterange', default: [START, END]}
                        }
                    ])
                }],
                _loadTplsFull: [{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwai790_table'),
                    url: g_ctxPath + 'crm/gwai790/getList',
                    exportsUrl: g_ctxPath + 'crm/gwai790/getList',
                    exportsName: moduleName + '数据',
                    title: moduleName + '信息表',
                    method: 'post',
                    id: 'gwai790_table',
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: table_where
                });

                var tipsIndex = null;

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwai790_table', where: table_where});
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwai790',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'COMPANY') {//法人开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'cq_sysorg_biz_1',
                                            columnList: [
                                                {label: '法人编号', prop: 'AZP01', width: '150'},
                                                {label: '法人名称', prop: 'AZP02', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.COMPANY = data.AZP01;
                                        },
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择法人信息',
                                        area: ['600px', '470px']
                                    }
                                });
                            }else if(field == 'GWDD012'){//客户
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwak001',
                                            columnList: [
                                                {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户名称', prop: 'GWAKL004', width: '150',sortable:false,unshow:true}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWDD012 = data.GWAK001;
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '465px']
                                    }
                                });
                            }else if(field == 'GWDD017_NAME'){//返利编号
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwde003',
                                            columnList: [
                                                { label: '选项值', prop: 'GWDE003', width: '150'},
                                                { label: '选项名称', prop: 'GWDE004', width: '150'}
                                            ],
                                            searchParam:{
                                                GWDE002: '返利编号',
                                            }
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWDD017_NAME = data.GWDE003;
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择返利编号',
                                        area: ['600px', '470px']
                                    }
                                });
                            }
                        }
                    },
                    watch:{
                        'isearch.fields.COMPANY': function (val) {
                            if (val == '' || val == null) {
                                COMPANY = '';
                            }else {
                                COMPANY = val;
                            }
                        },
                        'isearch.fields.DOCDATE': function (newValue, oldValue) {
                            if (newValue != null && newValue.length == 2) {
                                //判断时间跨度是否大于3个月
                                var arr1 = newValue[0].split('-');
                                var arr2 = newValue[1].split('-');
                                arr1[1] = parseInt(arr1[1]);
                                arr1[2] = parseInt(arr1[2]);
                                arr2[1] = parseInt(arr2[1]);
                                arr2[2] = parseInt(arr2[2]);
                                var flag = true;
                                if(arr1[0] == arr2[0]){//同年
                                    if(arr2[1]-arr1[1] > 3){ //月间隔超过3个月
                                        flag = false;
                                    }else if(arr2[1]-arr1[1] == 3){ //月相隔3个月，比较日
                                        if(arr2[2] > arr1[2]){ //结束日期的日大于开始日期的日
                                            flag = false;
                                        }
                                    }
                                }else{ //不同年
                                    if(arr2[0] - arr1[0] > 1){
                                        flag = false;
                                    }else if(arr2[0] - arr1[0] == 1){
                                        if(arr1[1] < 10){ //开始年的月份小于10时，不需要跨年
                                            flag = false;
                                        }else if(arr1[1]+3-arr2[1] < 12){ //月相隔大于3个月
                                            flag = false;
                                        }else if(arr1[1]+3-arr2[1] == 12){ //月相隔3个月，比较日
                                            if(arr2[2] > arr1[2]){ //结束日期的日大于开始日期的日
                                                flag = false;
                                            }
                                        }
                                    }
                                }
                                if(!flag) {
                                    app.showMessage("时间跨度不得超过3个月！");
                                    this.isearch.fields.DOCDATE = [START,END];
                                    return;
                                }else {
                                    this.isearch.fields.DOCDATE_START = newValue[0];
                                    this.isearch.fields.DOCDATE_END = newValue[1];
                                }
                            } else {
                                this.isearch.fields.DOCDATE_START = '';
                                this.isearch.fields.DOCDATE_END = '';
                            }
                        },
                    }
                }]
            });
            table_where.DOCDATE_END = END;
            table_where.DOCDATE_START = START;
            self.initTable();
            $('#itoSearch').click(function(){
                if(COMPANY == null || COMPANY == ''){
                    app.showMessage("法人必选！");
                    return ;
                }
            })
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

});